package com.alibaba.sample.kantiezi.biz.user.ao;

import com.alibaba.biz.command.ao.ApplicationObjectSupport;
import com.alibaba.biz.command.result.Result;
import com.alibaba.biz.command.result.ResultSupport;

import com.alibaba.common.lang.StringUtil;

import com.alibaba.sample.kantiezi.dal.dataobject.User;
import com.alibaba.sample.kantiezi.biz.user.ao.result.LoginResultCode;
import com.alibaba.sample.kantiezi.biz.user.manager.UserManager;

public class Login extends ApplicationObjectSupport {
    private UserManager userManager;
    private String      userId;
    private String      password;

    public void setUserManager(UserManager userManager) {
        this.userManager = userManager;
    }

    public void setUserId(String userId) {
        this.userId = userId;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void afterPropertiesSet() throws Exception {
        if (StringUtil.isEmpty(userId)) {
            throw new IllegalArgumentException("missing userId");
        }

        if (StringUtil.isEmpty(password)) {
            throw new IllegalArgumentException("missing password");
        }
    }

    public Result execute() {
        User   user   = userManager.login(userId, password);
        Result result = new ResultSupport();

        if (user == null) {
            result.setSuccess(false);
            result.setResultCode(LoginResultCode.INVALID_USER_OR_PASSWORD);
        } else {
            result.setDefaultModel("user", user);
        }

        return result;
    }
}
